// Ordinary service never dies unless stopSelf() called
// can transpose this to Intent Service
// a service does not create its own thread so use AsyncTask to create one
//
// 
// this async/service is more suited to a service that needs to interact with UI
// change to Intent service


package com.bren.android.androidproject;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.IBinder;

public class BootService extends Service{
	ScheduleEvent se = new ScheduleEvent();// object called on action time tick
	// Service doesn't start new thread hence AsyncTask
	AsyncTask<Void,Void,Void> myTask= new AsyncTask<Void,Void,Void>() {	
		protected Void doInBackground(Void... arg0) {
			android.util.Log.w("BootService", "BootServiceStarted");
			// register scheduleEvent to receive time tick
			registerReceiver(se, new IntentFilter(Intent.ACTION_TIME_TICK));
			android.util.Log.w("BootService","Registered ScheduleEvent for Time tick");
			// start ScheduleService, check immediately if scheduled for silence
			android.util.Log.w("BootService","Starting Schedule Service");
			Intent service=new Intent(BootService.this, ScheduleService.class);
			startService(service);
			// start MapService, check immediately if in silent map area
			// this intent throw null pointer exception!
			android.util.Log.w("BootService","Starting Map Service");
			service=new Intent(BootService.this, MapService.class);
			startService(service);
			return null;
		}
	};	
	
	public BootService() {
		super();
	}
	
	public void onCreate() {
		myTask.execute();// start once on creation
		// runs forever? no stop
	}
	
	public int onStartCommand(Intent i, int flags, int startId) {
		if (i!=null) {
			android.util.Log.w("BootService::onStartCommand", i.getStringExtra("extraData"));
		}else {
			android.util.Log.w("BootService,Recreated by", "System");
		}
		//stopSelf();// hopefully won't kill thread too soon?
		return Service.START_STICKY;// if killed don't try restart
	}

	public void onDestroy() {
		unregisterReceiver(se);
	}
	
	public IBinder onBind(Intent intent) {
		return null;
	}

}
